package edu.pinkhub.shiro_test.util;

import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;

import java.util.HashMap;
import java.util.Map;

/**
 * @author pinkhub
 * @create 2021-12-06 19:11
 * @ClassName DigestsUtil
 * @packageName edu.pinkhub.shrio_demo.util
 */
public class DigestsUtil {
    public static final String SHA1="SHA-1";
    public static final Integer ITERATION=512;
    /**sha1摘要算法
     * @param input 明文字符串
     * @param salt  干扰数据
     * @return
     */
    public static String sha1(String input,String salt){
        return new SimpleHash(SHA1,input,salt,ITERATION).toString();
    }
    /**
     * 随机生成salt
     * @return  salt(16进制)
     */
    public static String createSalt(){
        SecureRandomNumberGenerator secureRandomNumberGenerator = new SecureRandomNumberGenerator();
        return secureRandomNumberGenerator.nextBytes().toHex();
    }
    /**
     *生成密码和salt的密文
     * @param pwd
     * @return
     */
    public static Map<String,String> entryptPassword(String pwd){
        HashMap<String, String> map = new HashMap<>();
        String salt=createSalt();
        String password=sha1(pwd,salt);
        map.put("salt",salt);
        map.put("password",password);
        return map;
    }
}
